//
//  HomeViewController.m
//  ChatForHot
//
//  Created by FLY on 2018/10/9.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCollectionCell.h"
#import <CoreLocation/CoreLocation.h>
#import "UserInfoController.h"
#import "MJRefresh.h"
@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,CLLocationManagerDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, strong) CLLocationManager * HOTCHATLocation;

@property (nonatomic, strong) NSMutableDictionary* paramDic;

@property (nonatomic, strong) NSMutableArray* dataArr;

@property (nonatomic, strong) NSNumber* HOTCHATlongi;

@property (nonatomic, strong) NSNumber* HOTCHATlati;



@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = NSLocalizedString(@"local01", nil);
    
    
}

- (void)HOTCHATInsertSubViews{
    
    [self.view addSubview:self.HOTCHATcollectionView];
    
    [self.HOTCHATcollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.equalTo(self.view);
    }];
    //mj header
    MJRefreshNormalHeader *reHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.HOTCHATcollectionView.mj_footer endRefreshing];
        self.pageIndex = 1;
        [self HOTCHATbegainLocation];
    }];
    reHeader.stateLabel.hidden = YES;
    reHeader.lastUpdatedTimeLabel.hidden = YES;
    self.HOTCHATcollectionView.mj_header =reHeader;
    //mj footer
    MJRefreshAutoNormalFooter * reFooter= [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.HOTCHATcollectionView.mj_header endRefreshing];
        self.pageIndex ++;
        [self HOTCHATrequestHomeData];
    }];
    reFooter.stateLabel.hidden = YES;
    reFooter.refreshingTitleHidden = YES;
    self.HOTCHATcollectionView.mj_footer =reFooter;
    //begain request
    [self.HOTCHATcollectionView.mj_header beginRefreshing];
    
}

//lazzy
- (UICollectionView *)HOTCHATcollectionView{
    
    if (_HOTCHATcollectionView == nil) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//        layout.de
        _HOTCHATcollectionView =[[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _HOTCHATcollectionView.dataSource = self;
        _HOTCHATcollectionView.delegate = self;
        _HOTCHATcollectionView.backgroundColor = [UIColor clearColor];
        [_HOTCHATcollectionView registerClass:[HomeCollectionCell class] forCellWithReuseIdentifier:@"HomeCollectionCell"];
        
    }
    return _HOTCHATcollectionView;
}
- (NSMutableDictionary *)paramDic{
    
    if (_paramDic == nil) {
        
        _paramDic = [[NSMutableDictionary alloc]init];
    }
    return _paramDic;
}
- (NSMutableArray *)dataArr{
    
    if (_dataArr == nil) {
        
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
#pragma uicollectionDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UserInfoModel *model = self.dataArr[indexPath.row];
    
    HomeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionCell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    UserInfoModel *model = self.dataArr[indexPath.row];
    UserInfoController *VC = [[UserInfoController alloc]init];
    VC.IMID = model.IMUser;
    VC.accountId = model.accountid;
    VC.userInfoModel = model;
    [self.navigationController pushViewController:VC animated:YES];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake((HOTCHATUIWIDTH - 5*4)/2, (HOTCHATUIWIDTH - 5*4)/2);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}


//network
- (void)HOTCHATrequestHomeData{
    
    [self HOTCHATstartLoading];
    APPINFO *appInfo = [APPINFO defaultManager];
    NSString *urlStr = @"";
    if (appInfo.sex.integerValue == 0) {
        //request man
        urlStr = WOMENHOMELIST_URL;
        [self.paramDic setValue:@"1" forKey:@"sex"];
        [self.paramDic setValue:@"1" forKey:@"mold"];
        
            
    }else{
        //request womenapi
        urlStr = MANHOMELIST_URL;
        
        [self.paramDic setValue:@"0" forKey:@"sex"];
        [self.paramDic setValue:@"1" forKey:@"isFree"];
        
    }
    [self.paramDic setValue:appInfo.accountid forKey:@"accountid"];
    [self.paramDic setValue:@"20" forKey:@"pageSize"];
    [self.paramDic setValue:[NSNumber numberWithInteger:self.pageIndex] forKey:@"currentPage"];
    
    [NetWorkTool HOTCHATRequestWithStringURL:urlStr progress:^(NSProgress * _Nonnull progress) {
        
    } RequestWay:@"GET" Parma:self.paramDic SuccessBlock:^(BOOL isSucess, NSDictionary * _Nonnull resultDictionary) {
        [self HOTCHATstopLoading];
        [self endrefresh];
        if (self.pageIndex == 1) {
            
            [self.dataArr removeAllObjects];
        }
        if ([resultDictionary[@"code"] integerValue] == 0) {
            NSArray *dataArr = resultDictionary[@"data"];
            for (NSDictionary *dic in dataArr) {
                UserInfoModel *model = [[UserInfoModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.dataArr addObject:model];
            }

            [self.HOTCHATcollectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
        }
        
    } FailureBlock:^(NSError * _Nonnull error) {
        [self HOTCHATstopLoading];
        [self endrefresh];
    }];
}
- (void)endrefresh{
    
    [self.HOTCHATcollectionView.mj_header endRefreshing];
    [self.HOTCHATcollectionView.mj_footer endRefreshing];
}
#pragma location
- (void)HOTCHATbegainLocation{
    
    self.HOTCHATLocation = [[CLLocationManager alloc]init];
    self.HOTCHATLocation.delegate = self;
    [self.HOTCHATLocation requestWhenInUseAuthorization];
    [self.HOTCHATLocation startUpdatingLocation];
    
}
// CLLocationManager delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    // 1.get userinfo
    CLLocation *location = [locations lastObject];
    CLLocationCoordinate2D coordinate = location.coordinate;
    
    self.HOTCHATlongi = [NSNumber numberWithDouble:coordinate.longitude];
    self.HOTCHATlati = [NSNumber numberWithDouble:coordinate.latitude];
    
    [self.paramDic setValue:self.HOTCHATlongi forKey:@"muLongitud"];
    [self.paramDic setValue:self.HOTCHATlati forKey:@"muLatitude"];
    
    // stop location
    [manager stopUpdatingLocation];
    
    [self HOTCHATrequestHomeData];
}
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    if (error.code == kCLErrorDenied) {
        //error
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"access3", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"clip3", nil) otherButtonTitles:nil, nil];
        alertView.tag = 100;
        [alertView show];
    }
    [self HOTCHATrequestHomeData];
}
@end
